package fatworm.storage.table;

import java.util.*;

import fatworm.storage.types.*;

public class TableInfo
{
    private Schema schema;
    private Map<String, Integer> offsets;
    private int recordlen;
    private String dbname, tblname;

    public TableInfo(String dbname, String tblname, Schema schema)
    {
        this.dbname = dbname;
        this.schema = schema;
        this.tblname = tblname;
        offsets = new HashMap<String, Integer>();
        int pos = 0;
        for (String fldname : schema.fields())
        {
            offsets.put(fldname, pos);
            pos += schema.type(fldname).lengthInTuplePage();
        }
        recordlen = pos;
    }
    public TableInfo(String dbname, String tblname, Schema schema, Map<String, Integer> offsets,
            int recordlen)
    {
        this.dbname = dbname;
        this.tblname = tblname;
        this.schema = schema;
        this.offsets = offsets;
        this.recordlen = recordlen;
    }
    public String fileName()
    {
        return dbname + "/" + tblname + ".tbl";
    }
    public String varFileName(String fldname)
    {
        SType st = schema.type(fldname);
        if (st == null || Types.isVarType(st.getType()))
            throw new Error("!!!");
        return dbname + "/" + tblname + "." + fldname + ".tbl";
    }
    public Schema schema()
    {
        return schema;
    }
    public int offset(String fldname)
    {
        return offsets.get(fldname);
    }
    public int tupleLength()
    {
        return recordlen;
    }
}